package com.example.mygettingresult;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MyGettingResult extends Activity {
	public Button pick_content;
	
	static final int PICK_CONTACT_REQUEST = 1;  // The request code
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_getting_result);
		setView();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		// Check which request we're responding to
	    if (requestCode == PICK_CONTACT_REQUEST) {
	        // Make sure the request was successful
	        if (resultCode == RESULT_OK) {
	        	String number = getContentNumber(data);
	        	showNumber(number);
	        }
	    }
	}
	
	private void setView(){
		pick_content = (Button)findViewById(R.id.pick_content);
		pick_content.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				pickContact();
			}
		});
	}
	
	private void pickContact() {
		Uri content = Uri.parse("content://contacts");
	    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, content);
	    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
	    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
	}
	
	private String getContentNumber(Intent data){
		// Get the URI that points to the selected contact
        Uri contactUri = data.getData();
        // We only need the NUMBER column, because there will be only one row in the result
        String[] projection = {Phone.NUMBER};

        // Perform the query on the contact to get the NUMBER column
        // We don't need a selection or sort order (there's only one result for the given URI)
        // CAUTION: The query() method should be called from a separate thread to avoid blocking
        // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
        // Consider using CursorLoader to perform the query.
        Cursor cursor = getContentResolver()
                .query(contactUri, projection, null, null, null);
        cursor.moveToFirst();

        // Retrieve the phone number from the NUMBER column
        int column = cursor.getColumnIndex(Phone.NUMBER);
        String number = cursor.getString(column);
        return number;

        // Do something with the phone number...
	}
	
	private void showNumber(String number){
		Toast.makeText(getApplicationContext(), number, Toast.LENGTH_SHORT).show();
	}
}
